package com.wbao.dianniu.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.Mode;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.OnRefreshListener;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.adapter.IReleaseActiveAdapter;
import com.wbao.dianniu.data.ActivityData;
import com.wbao.dianniu.data.ActivityListData;
import com.wbao.dianniu.listener.IActivityListListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.ActivityListManager;
import com.wbao.dianniu.manager.ReleaseDataManager;
import com.wbao.dianniu.manager.ReleaseDataManager.IReleaseDelListener;
import com.wbao.dianniu.utils.Notification;
import com.wbao.dianniu.R;

/**
 * 我发布的活动
 * 
 * @author yejianjun
 * 
 */
public class IReleaseActivity extends BaseActivity implements IActivityListListener,IReleaseDelListener {
	private ActivityListManager manager;
	private PullToRefreshListView pullListView;
	private ListView listView;
	private final int PAGE_COUNTS = 10;
	private int currentPage = 0;
	private List<ActivityListData> mList = new ArrayList<ActivityListData>();
	private IReleaseActiveAdapter adapter;
	private TextView noDataTV;
	private final int TYPE = 3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentLayout(R.layout.activity_i_release,LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
		setTitleName(getResources().getString(R.string.i_released));
		initView();
		initData();
		ReleaseDataManager.getInstance().addListener(this);
	}

	private void initView() {
		noDataTV = (TextView) findViewById(R.id.no_find_data);
		pullListView = (PullToRefreshListView) findViewById(R.id.i_release_listview);
		pullListView.setMode(Mode.PULL_UP_TO_REFRESH);
		pullListView.setOnRefreshListener(onRefreshListener);
		listView = pullListView.getRefreshableView();
		registerForContextMenu(listView);
	}

	private void initData() {
		manager = new ActivityListManager(this);
		manager.addActivityListener(this);
		adapter = new IReleaseActiveAdapter(this);
		adapter.addData(mList);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(itemClickListener);
		reqData(0);
	}

	private void reqData(int page) {
		manager.activityReq(GlobalContext.accountId, page * PAGE_COUNTS,
				PAGE_COUNTS, "", TYPE);
	}

	OnRefreshListener onRefreshListener = new OnRefreshListener() {

		@Override
		public void onRefresh() {
			// 上拉刷新 加载请求数据
			pullUpRequest();
		}
	};

	// 上拉刷新数据请求
	private void pullUpRequest() {
		// 没有搜索结果 或者数据为空 上拉没有效果
		currentPage++;
		reqData(currentPage);
	}

	OnItemClickListener itemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			ActivityListData data = mList.get(position-1);
			Intent intent = new Intent();
			Bundle bundle = new Bundle();
			bundle.putString(GlobalContext.BUNDLE_BROWSE, data.getBrowseCount() + "");
			bundle.putString(GlobalContext.BUNDLE_SIGNUP, data.getInCount() + "");
			bundle.putString(GlobalContext.BUNDLE_PASS, data.getPassCount() + "");
			bundle.putInt(GlobalContext.BUNDLE_ID, data.getId());
			bundle.putString(GlobalContext.BUNDLE_ADDRESS, data.getAddress());
			bundle.putString(GlobalContext.BUNDLE_PRICE, data.getPrice() +"");
			bundle.putString(GlobalContext.BUNDLE_SUBJECT, data.getSubject());
			bundle.putString(GlobalContext.BUNDLE_MAXPERSION,data.getMaxPersion() +"");
			bundle.putString(GlobalContext.BUNDLE_STARTDATE, data.getStartDate());
//			bundle.putString(GlobalContext.BUNDLE_CONTENT, data.getContent());
			intent.putExtra(GlobalContext.BUNDLE_DATA, bundle);
			intent.setClass(IReleaseActivity.this, ActManagerActivity.class);
			startActivity(intent);
		}
	};

	@Override
	public void onActivityFailure(int errorCode, String errorMsg) {
		pullListView.onRefreshComplete(false, false);
		Notification.toast(this, errorMsg);
	}

	@Override
	public void onActivitySuccess(List<ActivityListData> list) {
		pullListView.onRefreshComplete(false, false);
		if (null != list) {
			adapter.addData(list);
			mList = adapter.getList();
			if (adapter.getCount() == 0) {
				noDataTV.setVisibility(View.VISIBLE);
			} else {
				noDataTV.setVisibility(View.GONE);
			}
			adapter.notifyDataSetChanged();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back_button:
			mList.clear();
			finish();
			break;
		default:
			break;
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		manager.removeActivityListener(this);
		ReleaseDataManager.getInstance().removeListener(this);
	}
	
	@Override
	public void removeOneData(int activityId) {
		for (int i = 0; i < mList.size(); i++) {
			if (activityId == mList.get(i).getId()) {
				pullListView.onRefreshComplete(false, false);
				mList.remove(i);
				mList = adapter.getList();
				if (adapter.getCount() == 0) {
					noDataTV.setVisibility(View.VISIBLE);
				} else {
					noDataTV.setVisibility(View.GONE);
				}
				adapter.notifyDataSetChanged();
				break;
			}
		}
	}
	@Override
	public void modifyOneData(int activityId, ActivityData data) {
		for (int i = 0; i < mList.size(); i++) {
			if(mList.get(i).getId() == data.id){
				mList.get(i).setSubject(data.subject);
				mList.get(i).setStartDate(data.startDate);
				mList.get(i).setMaxPersion(data.maxPersion);
				mList.get(i).setAddress(data.address);
				mList.get(i).setPrice(data.price);
				adapter.notifyDataSetChanged();
				break;
			}
		}
	}
	
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
		}
		public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
		}

}
